set(CMAKE_CXX_STANDARD 17)
set(CMAKE_AUTOUIC_SEARCH_PATHS (ui))

set(
    _srcs
    src/main.cpp
    src/qls_core_system.cpp
)

set(
    _headers
    include/core_system/qls_core_system.h
)

set(
    _uis
    ui/qls_core_system.ui
)

set(CMAKE_SKIP_BUILD_RPATH  FALSE)
set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH FALSE)    # 安装的执行文件加上 RPATH
set(BUILD_RPATH_USE_ORIGIN TRUE)

add_executable(${PROJECT_NAME} ${_srcs} ${_headers} ${_uis} ${QRC_SOURCE_FILES})
add_dependencies(${PROJECT_NAME} translations)
target_include_directories(${PROJECT_NAME} PRIVATE include)
target_link_libraries(${PROJECT_NAME} PUBLIC
    db_manager plugins_manager_appfwk
    Qt5::Widgets ${CMAKE_THREAD_LIBS_INIT}
)
target_link_libraries(${PROJECT_NAME} PRIVATE
    login_appfwk themes_manager languages_manager log settings
)
if (CMAKE_SYSTEM_NAME MATCHES "Linux")
    set_target_properties(${PROJECT_NAME} PROPERTIES
        LINK_FLAGS "-Wl,--export-dynamic"
        INSTALL_RPATH "\${ORIGIN}/../lib"
    )
elseif (CMAKE_SYSTEM_NAME MATCHES "Windows")
    set_target_properties(${PROJECT_NAME} PROPERTIES
        LINK_FLAGS "-Wl,--export-all-symbols"
        INSTALL_RPATH "\${ORIGIN}/../lib"
    )
endif()

if(qlpmsp_USE_PROFILER)
    target_compile_definitions(${PROJECT_NAME} PRIVATE -DUSE_EASY_PROFILER=1)
endif(qlpmsp_USE_PROFILER)

add_custom_command(
    TARGET ${PROJECT_NAME}
    PRE_BUILD
    COMMAND ${CMAKE_COMMAND} -E touch_nocreate ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
)

# 拷贝依赖到编译文件夹，用于 debug 调试
add_custom_command(
    TARGET ${PROJECT_NAME} POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E create_symlink
    ${CMAKE_CURRENT_SOURCE_DIR}/../../resources/ ${EXECUTABLE_OUTPUT_PATH}/../resources
)

install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION bin)

if(NOT Qt5Core_DIR)
  find_package(Qt5Core REQUIRED)
endif()
get_filename_component(QT_LINUX_QT_ROOT "${Qt5Core_DIR}/../../.." ABSOLUTE)

# 配置qt相关目录 https://doc.qt.io/qt-5/qt-conf.html
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/../../config/qt.conf DESTINATION bin)
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../../resources DESTINATION .)

install(CODE "set(QT_LINUX_QT_ROOT \"${QT_LINUX_QT_ROOT}\")")
install(CODE [[
file(INSTALL
  DESTINATION "${CMAKE_INSTALL_PREFIX}/lib"
  TYPE SHARED_LIBRARY
  FOLLOW_SYMLINK_CHAIN
  FILES
    "${QT_LINUX_QT_ROOT}/lib/libQt5XcbQpa.so.5"
    "${QT_LINUX_QT_ROOT}/lib/libQt5DBus.so.5"
)
]])

install(DIRECTORY ${QT_LINUX_QT_ROOT}/translations/ DESTINATION translations)
install(DIRECTORY ${QT_LINUX_QT_ROOT}/plugins/bearer DESTINATION plugins)
install(DIRECTORY ${QT_LINUX_QT_ROOT}/plugins/imageformats DESTINATION plugins)
install(DIRECTORY ${QT_LINUX_QT_ROOT}/plugins/platforms DESTINATION plugins)
install(DIRECTORY ${QT_LINUX_QT_ROOT}/plugins/xcbglintegrations DESTINATION plugins)
install(DIRECTORY ${QT_LINUX_QT_ROOT}/plugins/iconengines DESTINATION plugins)
install(DIRECTORY ${QT_LINUX_QT_ROOT}/plugins/platforminputcontexts DESTINATION plugins)
install(DIRECTORY ${QT_LINUX_QT_ROOT}/plugins/sqldrivers DESTINATION plugins)

